/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.listener.processor;

import lk.mrt.cse.wihidum.listener.storage.*;
import org.apache.axiom.om.*;
import org.apache.axiom.soap.*;
import org.apache.log4j.*;

import javax.xml.namespace.*;
import java.util.*;

/**
 * Process get publications messages
 */
public class GetPublicationsProcessor
        implements Processor
{
    private static final Logger logger = Logger.getLogger (GetPublicationsProcessor.class);

    public OMElement processMessage (SOAPEnvelope requestElement)
    {
        try
        {
            logger.info ("Processing get publications message");

            OMElement subscriptionIDElement = requestElement.getBody().getFirstChildWithName(new QName("GetPublicationsRequest")).getFirstChildWithName (new QName ("subscriptionID"));
            String subscriptionID = subscriptionIDElement.getText ();

            DeliveryElementManager deliveryElementManager = new DeliveryElementDAO ();
            ArrayList<DeliveryElement> deliveryElements = deliveryElementManager.getDeliveryElements (subscriptionID);

            return createGetPublicationsResponseElement (deliveryElements);
        }
        catch (OMException ex)
        {
            logger.error ("Error in processing subscription message", ex);
            return createGetPublicationsFaultResponseElement ();
        }
    }

    /**
     * Create response for a valid get publications
     *
     * @param deliveryElements List of delivered elements
     * @return Response element
     */
    private OMElement createGetPublicationsResponseElement (ArrayList<DeliveryElement> deliveryElements)
    {
        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory ();

        OMElement responseElement = factory.createOMElement ("GetPublicationsResponse", null);
        OMElement referenceParameters = factory.createOMElement ("ReferenceParameters", null);
        OMElement responseTextElement = factory.createOMElement ("Response", null);
        responseTextElement.setText ("Get publications completed successfully");

        for (DeliveryElement deliveryElement : deliveryElements)
        {
            OMElement publicationElement = factory.createOMElement ("Publication", null);

            OMElement publisher = factory.createOMElement ("publisherAddress", null);
            publisher.setText (deliveryElement.getPublisherService ());
            publicationElement.addChild (publisher);

            OMElement topic = factory.createOMElement ("topic", null);
            topic.setText (deliveryElement.getTopic ());
            publicationElement.addChild (topic);

            OMElement publicationText = factory.createOMElement ("publicationText", null);
            publicationText.setText (deliveryElement.getPublicationText ());
            publicationElement.addChild (publicationText);

            responseTextElement.addChild (publicationElement);
        }

        referenceParameters.addChild (responseTextElement);
        responseElement.addChild (referenceParameters);

        return responseElement;
    }

    /**
     * Create response for an invalid get publications
     *
     * @return Response element
     */
    private OMElement createGetPublicationsFaultResponseElement ()
    {
        return null;
    }
}

